home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1988 / 12 / os2io.asc < prev    next >
Text File  |  1988-12-31  |  5KB  |  184 lines

  1. _WRITING OS/2 APPLICATIONS WITH I/O PRIVILEGES_
  2. by Ray Duncan
  3.  
  4. [EXAMPLE 1]
  5.  
  6.         title   PORTIO.ASM  read/write I/O ports
  7.         page    55,132
  8.         .286
  9.  
  10. ; PORTIO.ASM -- general purpose port read/write
  11. ;               routines for C or MASM programs
  12. ;
  13. ; Copyright (C) 1988 Ray Duncan
  14. ;
  15. ; When this module is linked into a program, the
  16. ; following lines must be present in the program's
  17. ; module definition (.DEF) file:
  18. ;
  19. ; SEGMENTS
  20. ;    IO_TEXT IOPL
  21. ;
  22. ; EXPORTS
  23. ;    rport 1
  24. ;    wport 2
  25. ;
  26. ; The SEGMENT and EXPORT directives are recognized by 
  27. ; the Linker and cause information to be built into
  28. ; the .EXE file header for the OS/2 program loader.
  29. ; The loader is signalled to give I/O privilege to 
  30. ; code executing in the segment IO_TEXT, and to build
  31. ; call gates for the routines 'rport' and 'wport'.
  32.  
  33. IO_TEXT segment word public 'CODE'
  34.         
  35.         assume  cs:IO_TEXT
  36.  
  37.  
  38. ; RPORT: read 8-bit data from I/O port.  Port address 
  39. ; is passed on stack, data is returned in register AX
  40. ; with AH zeroed.  Other registers are unchanged.
  41. ; C syntax:     unsigned port, data;
  42. ;               data = rport(port);
  43.  
  44.         public  rport
  45. rport   proc    far
  46.  
  47.         push    bp              ; save registers and
  48.         mov     bp,sp           ; set up stack frame
  49.         push    dx
  50.  
  51.         mov     dx,[bp+6]       ; get port number 
  52.         in      al,dx           ; read the port
  53.         xor     ah,ah           ; clear upper 8 bits
  54.                 
  55.         pop     dx              ; restore registers
  56.         pop     bp
  57.  
  58.         ret     2               ; discard parameters,
  59.                                 ; return port data in AX
  60. rport   endp
  61.  
  62.  
  63. ; WPORT: write 8-bit data to I/O port.  Port address and 
  64. ; data are passed on stack.  All registers are unchanged.
  65. ; C syntax:     unsigned port, data;
  66. ;               wport(port, data);
  67.  
  68.         public  wport
  69. wport   proc    far
  70.  
  71.         push    bp              ; save registers and
  72.         mov     bp,sp           ; set up stack frame
  73.         push    ax
  74.         push    dx
  75.  
  76.         mov     ax,[bp+6]       ; get data to write
  77.         mov     dx,[bp+8]       ; get port number
  78.         out     dx,al           ; write the port
  79.  
  80.         pop     dx              ; restore registers
  81.         pop     ax
  82.         pop     bp
  83.  
  84.         ret     4               ; discard parameters,
  85.                                 ; return nothing
  86. wport   endp
  87.  
  88. IO_TEXT ends
  89.  
  90.         end
  91.  
  92.  
  93. [EXAMPLE 2]
  94.  
  95.  
  96. /*
  97.     PORTS.C:    Demonstration program with IOPL.  
  98.                 Reads and displays the first 256 I/O ports.
  99.                 Requires separate module PORTIO.ASM.
  100.  
  101.     (C) 1988 Ray Duncan
  102.  
  103.     To build:   MASM /Mx PORTIO;
  104.                 CL /c PORTS.C
  105.                 LINK PORTS+PORTIO,PORTS,,,PORTS.DEF
  106.  
  107.     Usage:      PORTS
  108. */
  109.  
  110. #include <stdio.h>
  111.  
  112. #define API extern far pascal
  113.  
  114. unsigned API rport(unsigned);           /* function prototypes */
  115. void     API wport(unsigned, unsigned);
  116. void     API DosSleep(unsigned long);
  117. unsigned API DosPortAccess(unsigned, unsigned, unsigned, unsigned);
  118.  
  119.                                 /* parameters for DosPortAccess */
  120. #define REQUEST 0                       /* request port */
  121. #define RELEASE 1                       /* release port */
  122. #define BPORT   0                       /* beginning port */
  123. #define EPORT   255                     /* ending port */
  124.  
  125. main(int argc, char *argv[])
  126. {
  127.     int i;                              /* scratch variable */
  128.  
  129.                                         /* request port access */
  130.     if(DosPortAccess(0, REQUEST, BPORT, EPORT))
  131.     {   
  132.         printf("\nDosPortAccess failed.\n");
  133.         exit(1);
  134.     }
  135.  
  136.     printf("\n      ");                 /* print title line */
  137.     for(i=0; i<16; i++) printf(" %2X", i);
  138.  
  139.     for(i=BPORT; i<=EPORT; i++)         /* loop through all ports */
  140.     {
  141.         if((i & 0x0f)==0)
  142.         {
  143.             printf("\n%04X  ", i);      /* new line needed */
  144.         }
  145.  
  146.         printf(" %02X", rport(i));      /* read & display port */
  147.     }
  148.                                         /* release port access */
  149.     DosPortAccess(0, RELEASE, BPORT, EPORT);
  150. }
  151.  
  152.  
  153.  
  154. [EXAMPLE 3]
  155.  
  156. NAME PORTS WINDOWCOMPAT
  157.  
  158. PROTMODE
  159.  
  160. SEGMENTS
  161.   IO_TEXT IOPL
  162.  
  163. EXPORTS
  164.   rport 1
  165.   wport 2
  166.  
  167.  
  168.  
  169. [EXAMPLE 4]
  170.  
  171. ports.obj : ports.c
  172.   cl /c ports.c
  173.  
  174. portio.obj : portio.asm
  175.   masm /Mx portio;
  176.  
  177. ports.exe : ports.obj portio.obj ports.def
  178.   link ports+portio,ports,,,ports.def
  179.  
  180.  
  181.  
  182.